
import '../models/post.dart';
import '../models/comment.dart'; // 导入 Comment 模型
import 'dart:math'; // 用于生成随机数

class DataRepository {
  static const int _pageSize = 10;  // 添加分页大小常量
  static final Random _random = Random(); // 用于生成随机评论数

  static final List<Post> _mockPosts = List.generate(
    50,
    (index) {
      // 生成模拟评论
      int commentCount = _random.nextInt(15); // 随机生成 0-14 条评论
      List<Comment> mockComments = List.generate(
        commentCount,
        (commentIndex) => Comment(
          commentId: "comment_${index}_$commentIndex",
          userId: "user_${_random.nextInt(10)}",
          userNickname: "评论用户 ${commentIndex % 5}",
          userAvatarUrl: "https://via.placeholder.com/150/comment${commentIndex % 5}",
          content: "这是第 $commentIndex 条评论，针对帖子 $index。",
          ctime: DateTime.now().subtract(Duration(minutes: commentIndex * 5 + index)),
        ),
      );

      return Post(
        postId: "post_$index",
        title: "帖子标题 $index",
        content: "这是帖子内容 $index，可能会很长，需要省略显示。",
        userId: "user_${index % 10}",
        userNickname: "用户昵称 ${index % 10}",
        userAvatarUrl: "https://via.placeholder.com/150/$_userAvatarSeed${index % 10}",
        ctime: DateTime.now().subtract(Duration(days: index)),
        images: index % 3 == 0 ? ["https://lf6-bot-platform-tos-sign.coze.cn/bot-studio-bot-platform/quick_start/quick_start_3.jpeg?lk3s=50ccb0c5&x-expires=1745463613&x-signature=gkBExm%2FGbSFPdDpm6zW4kghDOlk%3D",
        "https://lf6-bot-platform-tos-sign.coze.cn/bot-studio-bot-platform/quick_start/release_note_3.jpeg?lk3s=50ccb0c5&x-expires=1745463613&x-signature=oEBNmoG%2Bn0x8Czik39dVaJg%2FrMw%3D"] : [],
        videoUrl: index % 5 == 0 ? "https://flutter.github.io/assets-for-api-docs/assets/videos/bee.mp4" : null,
        isFollowed: index % 2 == 0,
        isLiked: false,
        likesCount: index % 10,
        commentsCount: commentCount, // 使用生成的评论数
        comments: mockComments, // 添加生成的评论列表
      );
    }
  );
  // 用于生成不同占位头像的种子
  static const String _userAvatarSeed = "avatar";


  static Future<List<Post>> fetchPosts({required int page, int tab = 0, String? userId}) async {
    await Future.delayed(const Duration(seconds: 1)); // Simulate network delay
    final start = (page - 1) * _pageSize;

    if (userId != null) {
      // 按用户ID过滤帖子
      final userPosts = _mockPosts.where((post) => post.userId == userId).toList();
      if (start >= userPosts.length) {
        return [];
      }
      return userPosts.skip(start).take(_pageSize).toList();
    } else if (tab == 0) {
      // "关注" tab: 先过滤后分页
      final followedPosts = _mockPosts.where((post) => post.isFollowed).toList();
      if (start >= followedPosts.length) {
        return [];
      }
      return followedPosts.skip(start).take(_pageSize).toList();
    } else {
      // "发现" tab: 直接分页返回所有帖子
      if (start >= _mockPosts.length) {
        return [];
      }
      return _mockPosts.skip(start).take(_pageSize).toList();
    }
  }

  // 新增：获取视频帖子
  static Future<List<Post>> fetchVideoPosts({required int page}) async {
    await Future.delayed(const Duration(milliseconds: 500)); // Simulate network delay
    final videoPosts = _mockPosts.where((post) => post.videoUrl != null).toList();
    final start = (page - 1) * _pageSize;

    if (start >= videoPosts.length) {
      return [];
    }
    return videoPosts.skip(start).take(_pageSize).toList();
  }
  // fetchPosts 和 fetchVideoPosts 不需要修改，因为 Post 对象现在包含评论了
}
